package com.txzt.gaea.upgrade.result.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.aspect.annotation.SysDataPermission;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.upgrade.result.domain.model.ResultBorrow;
import com.txzt.gaea.upgrade.result.service.IResultBorrowService;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/result/borrow")
public class ResultBorrowController extends BaseController {

    @Autowired
    private IResultBorrowService resultBorrowService;


    /**
     * 分页查询
     *
     * @param resultBorrow
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                                      @RequestParam(defaultValue = "10") Integer pageSize, ResultBorrow resultBorrow) {
        PageQuery<ResultBorrow> pageList = resultBorrowService.pageQuery(new PageQuery<>(pageNumber, pageSize, resultBorrow));
        pageList.getList().forEach(item ->{
            //翻译借阅人
            item.setCreateByName(TranslateUtil.getUserName(item.getCreateBy()));
            //翻译负责人
            item.setProjectLeaderName(TranslateUtil.getUserName(item.getProjectLeader()));
            //翻译承担部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 查询待审核数量(成果借阅管理)
     * @param resultBorrow
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/listCheckCount")
    public RtnInfo<?> listCheckCount(ResultBorrow resultBorrow) {
        Long count = resultBorrowService.listCheckCount(resultBorrow);
        return RtnInfo.success(count);
    }

    /**
     * 新增
     * @param resultBorrow
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody ResultBorrow resultBorrow) {
        int count = resultBorrowService.addResultBorrow(resultBorrow);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        ResultBorrow resultBorrow = new ResultBorrow();
        resultBorrow.setId(id);
        resultBorrow.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = resultBorrowService.updateById(resultBorrow);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

//
//
//    @GetMapping(value = "/test")
//    public void test() throws IOException {
//        InputStream inputStream = WordUtils.class.getClassLoader().getResourceAsStream("static/计划编报模版.docx");
////        ClassPathResource resource = new ClassPathResource("./");
////        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/计划编报模版.docx");
//        if (null == inputStream) {
//            System.out.println("------------------------- Not found -------------------------");
//            return;
//        }
//        File file = new File("test.docx");
//        FileUtils.copyInputStreamToFile(inputStream,file);
//        if (file.exists()) {
//            System.out.println("------------------------- Yes hhhhhh -------------------------");
//        }else {
//            System.out.println("------------------------- No fxxkk -------------------------");
//        }
//    }
}
